package com.airforcehospital;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

@Slf4j
public class AirHttpTool {

    public static String cookie = "connect.sid=s%3AWuwugT4vwHl4LK9Vp-CJHvkRVssVbdjF.I3Ti6xXaEixBo8Ctiz1%2BtyerAgUVEo7fOTrmxSQ%2Frgs; acw_tc=0a47314817333797977444690e004c7f59bad722bdaeba08f6ac21c82c455f";
    static CloseableHttpClient httpclient = HttpClients.createDefault();
    static HttpGet get = new HttpGet();
    static HttpPost post = new HttpPost();

    public static void main(String[] args) throws Exception {
        post("",null);
    }
    // 改id  改签名
    //14591
    //private static String entityStr = "{\"p\":\"w\",\"Sign\":\"RootSign\",\"active_time_id\":\"14591\",\"contact_phone\":\"18310365230\",\"alternate\":\"\",\"toi_cardtype_id\":1,\"spot\":0,\"person\":[{\"toi_cardtype_id\":1,\"toi_username\":\"蒋汪洋\",\"toi_card_num\":\"332624199104240010\"}],\"notes\":\"\",\"children\":[],\"toi_unit\":\"\",\"uid\":\"P%3AhWT596H%2B3%2BICj%2BRVlOzsdA%3D%3D\",\"_sign\":[\"Az2vjNwtgm8ip8EgHaIPTkEbNOpv26IWXCb//6V1BH3J0Vj3JTCTpQcgp6gRhPrMGpIR0ispLYhq2SqUWCEB9RFQdadoTKttJV19GTL2fdiIA7nGvjDHGmVxjmI/LyAdjlbMFr/l9AFtct2Vo2c7v8WQEdHe5jgMVJnWSGQLg9j8SFEZRFnstZfvfKuFkOBm7yNMrbfLRrj3FYOhc2VEILf5TMBAVC3tUw/BuaGqnYqffriJ6GXXXifIZfIp+ATlH8lcQgWUslp8LJ0KrhDt8cYiUyDEP8agP+bJN4JSIHVybspN0NJAHpIkrmwG15Jc2qTo0Y4q4XtaPT/uD758qw==\"],\"api_token\":\"efd69a911cb0df16522d0c9349273914\"}";
    static{
        post.setHeader("Referer","https://m.hsyuntai.com/med/hp/hospitals/100578/hos/registration/doctorDetails/0/236/1318797/290198681?date=2024-11-26");
        post.setHeader("Accept-Encoding","gzip, deflate, br");
        post.setHeader("accept","application/json, text/plain, */*");
        post.setHeader("yt-baseinfo","%7B%22openid%22%3A%22ofsEA1r97u0g6MTjykbZg-MezWfY%22%2C%22thirdAppId%22%3A%22wx1d99f89bd913773e%22%2C%22access_token%22%3Atrue%2C%22appId%22%3A%22100578%22%7D");
        post.setHeader("openid","ofsEA1r97u0g6MTjykbZg-MezWfY");
        post.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        post.setHeader("x-requested-with","XMLHttpRequest");
        post.setHeader("tt_upload_params","[object Object]");
        post.setHeader("ngversion","99.2.2");
        post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c11) XWEB/11275 Flue");
        post.setHeader("Sec-Fetch-Dest","empty");
        post.setHeader("If-None-Match","W/\"M8kad2IFoc249pkMyy6IWg==\"");
        post.setHeader("unicode","b5tMG7YNVTRIQzllll95rsuEs0lL3u");
        post.setHeader("Host","m.hsyuntai.com");
        post.setHeader("Sec-Fetch-Mode","cors");
        post.setHeader("Sec-Fetch-Site","same-origin");
        post.setHeader("Connection","keep-alive");
        post.setHeader("Cookie",cookie);

        get.setHeader("Accept-Encoding","gzip, deflate, br");
        get.setHeader("accept","application/json, text/plain, */*");
        get.setHeader("yt-baseinfo","%7B%22openid%22%3A%22ofsEA1r97u0g6MTjykbZg-MezWfY%22%2C%22thirdAppId%22%3A%22wx1d99f89bd913773e%22%2C%22access_token%22%3Atrue%2C%22appId%22%3A%22100578%22%7D");
        get.setHeader("openid","ofsEA1r97u0g6MTjykbZg-MezWfY");
        get.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        get.setHeader("x-requested-with","XMLHttpRequest");
        get.setHeader("tt_upload_params","[object Object]");
        get.setHeader("ngversion","99.2.2");
        get.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x63090c11) XWEB/11275 Flue");
        get.setHeader("Sec-Fetch-Dest","empty");
        get.setHeader("If-None-Match","W/\"M8kad2IFoc249pkMyy6IWg==\"");
        get.setHeader("unicode","b5bWuCmSr1KvQzllllB5rsuEkUlouY");
        get.setHeader("Host","m.hsyuntai.com");
        get.setHeader("Sec-Fetch-Mode","cors");
        get.setHeader("Sec-Fetch-Site","same-origin");
        get.setHeader("Connection","keep-alive");
        get.setHeader("Cookie",cookie);

        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(3000) // 连接超时时间，单位毫秒
                .setSocketTimeout(3000)  // 读取超时时间，单位毫秒
                .build();
        post.setConfig(requestConfig);
        get.setConfig(requestConfig);

    }
    public static Object post(String url,JSONObject data) throws Exception {

        CloseableHttpResponse response ;
        post.setURI(new URI(url));

        if(Objects.nonNull(data)){
            post.setEntity(new StringEntity(JSON.toJSONString(data), ContentType.APPLICATION_JSON));
        }

        try{
            response = httpclient.execute(post);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            if(body.startsWith("[")){
                JSONArray resultArr = JSONArray.parseArray(body);
                return resultArr;
            }else if(body.startsWith("{")){
                JSONObject result = JSONObject.parseObject(body);
                return result;
            }else {
                return body;
            }

            //result = JSONArray.parseArray(body);
            //System.out.println(result);
        }catch (Exception e){
            log.error("请求接口出错"+e.toString());
            e.printStackTrace();
        }

        return null;
    }

    public static Object get(String url) throws Exception {

        CloseableHttpResponse response ;
        get.setURI(new URI(url));

        try{
            response = httpclient.execute(get);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            if(body.startsWith("[")){
                JSONArray resultArr = JSONArray.parseArray(body);
                return resultArr;
            }else if(body.startsWith("{")){
                JSONObject result = JSONObject.parseObject(body);
                return result;
            }

            //result = JSONArray.parseArray(body);
            //System.out.println(result);
        }catch (Exception e){
            log.error("请求接口出错"+e.toString());
            e.printStackTrace();
        }

        return null;
    }

}
